package kitwee.com.kanpad.orderDetail;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.LogUtils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.lidong.pdf.PDFView;
import com.lidong.pdf.listener.OnDrawListener;
import com.lidong.pdf.listener.OnLoadCompleteListener;
import com.lidong.pdf.listener.OnPageChangeListener;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Optional;
import kitwee.com.kanpad.BaseFragment;
import kitwee.com.kanpad.R;

/**
 * @author GanQuan
 * @date 2018/1/22
 */

public class SOPFragment extends BaseFragment implements OnPageChangeListener, OnLoadCompleteListener, OnDrawListener {

    @BindView(R.id.zoom)
    ImageView zoom;
    @BindView(R.id.narrow)
    ImageView narrow;
    @BindView(R.id.pdf_view2)
    PDFView pdfView;
    private int width;
    private int height;
    private ViewGroup.LayoutParams layoutParams;

    private WebView mSopView;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View layout = inflater.inflate(R.layout.sop_view, container, false);
        ButterKnife.bind(this, layout);
        return layout;
//        mSopView =new WebView(getActivity());
//        mSopView.setLayoutParams(
//                new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
//        );
//        return mSopView;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    private void displayFromFile1(String fileUrl, String fileName) {
        pdfView.fileFromLocalStorage(this, this, this, fileUrl, fileName);   //设置pdf文件地址
    }


    public static SOPFragment newInstance() {
        return new SOPFragment();
    }

    @Override
    public void onStop() {
        super.onStop();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void onStart() {
        super.onStart();
        EventBus.getDefault().register(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onShowMessageEvent(OrderDetailEvent orderDetailEvent) {
//        LogUtils.e("SOP : " + orderDetailEvent.getListDetailBean().getSop_file_path());
//        if (orderDetailEvent.getListDetailBean().getSop_file_path() != null) {
//            displayFromFile1(orderDetailEvent.getListDetailBean().getSop_file_path(), "ee.pdf");
////            mSopView.loadData(orderDetailEvent.getListDetailBean().getSop_file_path(),"text/html;charset=UTF-8",null);
//        } else {
//            return;
//        }


    }


    @Override
    public void onPageChanged(int page, int pageCount) {

    }

    @Override
    public void loadComplete(int nbPages) {

    }

    @Override
    public void onLayerDrawn(Canvas canvas, float pageWidth, float pageHeight, int displayedPage) {
        LogUtils.e("width : " + pageWidth + " ; height : " + pageHeight);
        if (clickCount == 0) {
            width = (int) pageWidth;
            height = (int) pageHeight;
        }

    }

    private int MAX_ZOOM = 3;
    private int MID_ZOOM = 2;
    private int MIN_ZOOM = 1;


    private boolean isZOOM;

    private int clickCount = 0;

    @OnClick({R.id.zoom, R.id.narrow})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.zoom:

                if (clickCount >= 2) {
                    clickCount = 2;
                    return;
                } else {
                    clickCount++;
                    layoutParams = pdfView.getLayoutParams();
                    layoutParams.width = (int) (2f * width);
                    layoutParams.height = (int) (2f * height);
                    pdfView.setLayoutParams(layoutParams);
                }

                break;
            case R.id.narrow:
                clickCount--;
                layoutParams.width = width;
                layoutParams.height = height;
                pdfView.setLayoutParams(layoutParams);

                break;
        }
    }


}
